import { call, put, takeEvery } from 'redux-saga/effects' //1、引入saga下面的effects文件，将对于的api引入
import {getAllServe,addServeType,changeOneServeType,delManyServeType,
    getAllGoodType,addOneGoodType,delManyGoodType}  from '../../apis/hangApi'
import  {initAllServeType,initAddType,initChangeType,initDelType
,initAllGoodsType,initAddGoodType,initDelGoodType} from '../actions/index.js'
import api from './../../apijlf/api.js'
import { inituserAc, initdeptAc, initroleAc, initmenuAc } from './../actions'

// import { call, put, takeEvery } from "redux-saga/effects"
import { login,shop } from "../../apis/login"
import {getCharge} from "../../apis/charge"
import {getApply} from "../../apis/apply"
import {getShop} from "../../apis/shop"
import storageUtils from '../../utils/storageUtils';
import {message} from "antd"


export default function* index() {
    //服务类型
    yield takeEvery("getServeType",getAllServeType)
    yield takeEvery("addType",addType)
    yield takeEvery("searchServeType",searchServe)
    yield takeEvery("changeType",changeServe)
    yield takeEvery("delSomeServeType",delSomeType)

    //商品类型
    yield takeEvery("getGoodsType",getGoodType)
    yield takeEvery("addOneGoodType",addGoodType)
    yield takeEvery("searchGoodType",searchGoodType)
    yield takeEvery("delGoodType",delGoodType)

    yield takeEvery("loginSaga", asyncLogin)
    yield takeEvery("shopSaga", asyncShop)
    yield takeEvery("getChargeSaga", asyncCharge)
    yield takeEvery("getApplySaga", asyncApply)
    yield takeEvery("getShopSaga", asyncShops)

    //获取用户数据
    yield takeEvery('getUser', checkItem)
        //获取部门数据
    yield takeEvery('getDept', checkItem2)
        //获取所有角色数据
    yield takeEvery('getRole', checkItem3)
        //新增用户数据
    yield takeEvery('addUser', checkItem4)
        //删除用户
    yield takeEvery('delUser', checkItem5)
        //获取所有菜单
    yield takeEvery('getMenu', checkItem6)
        //新增角色
    yield takeEvery('addRole', checkItem7)
        //删除角色
    yield takeEvery('delRole', checkItem8)
}

// 登录模块
function* asyncLogin( payload ) {
    const res = yield call(login, payload)
    console.log(res);
    const { token, user} = res.data.data
    const {message:messages} = res.data
    console.log(message);
    if (messages == '认证成功') {
        storageUtils.setStorage('token', token);
        storageUtils.setStorage('userInfo', user);
        message.success('登陆成功');
      }
    // 执行派发
    yield put({ type: "initUser", payload: { token, user,messages } })
}

//商家入驻
function* asyncShop(payload) {
    const res = yield call(shop, payload)
    console.log(res);
    if(res.data.result == true){
        message.success("申请成功")
    }
}

//获取所有充电站
function* asyncCharge(payload) {
    const res = yield call(getCharge, payload)
    // console.log(res);
    const {rows} = res.data
    yield put({ type: "initCharge", payload: {rows} })
}

//获取所有店铺审核数据
function* asyncApply(payload){
    const res = yield call(getApply,payload)
    const {rows} = res.data
    yield put({type:"initApply",payload:{rows}})
}

//获取所有门店数据
function* asyncShops(payload){
    const res = yield call(getShop,payload)
    console.log(res);
    const {rows} = res.data
    yield put({type:"initShop",payload:{rows}})
}

//获取所有商品类型
function *getGoodType(params) {
    const res = yield call(getAllGoodType);
    yield put(initAllGoodsType(res.data.rows.children))
}

//新增商品类型
function *addGoodType({payload}) {
    const res = yield call(addOneGoodType,payload);
    yield put(initAddGoodType(res.status))
}

//获取所有商品类型
function *searchGoodType({payload}) {
    const res = yield call(getAllGoodType,payload);
    yield put(initAllGoodsType(res.data.rows.children))
}

//删除商品类型
function *delGoodType({payload}) {
    console.log(payload);
    const res = yield call(delManyGoodType,payload);
    console.log(res);
    yield put(initDelGoodType(res.status))
}




//获取所有的服务类型
function *getAllServeType(params) {
    const res = yield call(getAllServe);
    yield put(initAllServeType(res.data.rows.children))
}

//新增服务类型
function *addType({payload}) {
    const res = yield call(addServeType,payload);
    yield put(initAddType(res.status))
}

//搜索服务类型
function *searchServe({payload}) {
    const res = yield call(getAllServe,payload);
    yield put(initAllServeType(res.data.rows.children))
}

//搜索服务类型
function *changeServe({payload}) {
    console.log(payload);
    const res = yield call(changeOneServeType,payload);
    console.log(res);
    yield put(initChangeType(res.status))
}

//获取用户数据
function* checkItem({ payload }) {

    const { data } = yield call(api.users.user, payload);
    const usersArr = data
    yield put(inituserAc(usersArr))
};
//获取部门数据
function* checkItem2() {

    const { data } = yield call(api.users.dept);
    const deptArr = data.rows.children
    yield put(initdeptAc(deptArr))
};
//获取角色数据
function* checkItem3({ payload }) {

    const { data } = yield call(api.users.role, payload);
    yield put(initroleAc(data))
};
//新增用户
function* checkItem4({ payload }) {

    const data = yield call(api.users.addUser, payload);
};
//删除用户
function* checkItem5({ payload }) {

    const data = yield call(api.users.delUser, payload);
};
//获取所有菜单
function* checkItem6({ payload }) {
        const { data } = yield call(api.users.menu, payload);
        yield put(initmenuAc(data))
    }
    //新增角色
function* checkItem7({ payload }) {
        const data = yield call(api.users.addRole, payload);
    }
    //删除角色
function* checkItem8({ payload }) {

    const data = yield call(api.users.delRole, payload);
};

//删除服务类型
function *delSomeType({payload}) {
    console.log(payload);
    const res = yield call(delManyServeType,payload);
    console.log(res);
    yield put(initDelType(res.status))
}